package org.scc.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.scc.model.Group;
import org.scc.model.User;

/**
 * 组的数据访问类
 * @author rzpeng
 *
 */
public class GroupDao extends BaseDao{
	
	@Override
	protected String getMapper(String methodName) {
		return "org.scc.sqlmap.GroupMapper." + methodName;
	}
	
	/**
	 * 新建组
	 * @param group
	 */
	public void add(Group group){
		insert( getMapper("add"), group, "add group error");
	}
	
	/**
	 * 更新组
	 * @param group
	 */
	public void update(Group group){
		update( getMapper("update"), group, "update group error");
	}
	
	/**
	 * 删除组
	 * @param ids
	 */
	public void delete(String ids){
		delete(getMapper("delete"), ids, "delete Group error");
	}
	
	/**
	 * 加载一个组
	 * @param id
	 * @return group
	 */
	public Group load(int id){
		return selectOne(getMapper("load"), id, "load Group error");
	}
	
	/**
	 * 查询所有的组
	 * @return
	 */
	public List<Group> findAll(){
		return selectList(getMapper("findAll"), "findAll group error");
	}
	
	/**
	 * 添加组员
	 * @param groupId 组id
	 * @param userIds 组员id拼串逗号分割
	 */
	public void addUsers(int groupId, String userIds){
		Map<String, Integer> params = new HashMap<String, Integer>();
		String[] ids = userIds.split(",");
		for(String sid : ids) {
			params.put("groupId", groupId);
			params.put("userId", Integer.parseInt(sid) );
			insert(getMapper("addUsers"), params, "add user to group error");
			params.clear();
		}
	}
	
	/**
	 * 查询组员列表
	 * @param groupId
	 * @return
	 */
	public List<User> findUsers(int groupId){
		return selectList(getMapper("findGroupUsers"), groupId, "find group user error");
	}
	
	/**
	 * 按类型查询组
	 * @param type
	 * @return
	 */
	public List<Group> findByType(int type){
		return selectList(getMapper("findByType"), type, "find group by type error");
	}
	
	/**
	 * 删除小组的所有人员
	 * @param groupId
	 */
	public void deleteUsers(int groupId){
		delete(getMapper("deleteGroupUser"), groupId, "delete user of group error");
	}
}
